package com.crack.nga2.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.crack.nga2.R;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 表情选择
 * Created by Administrator on 2016/1/12.
 */
public class EmotionFragment extends Fragment implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    /**
     * 表情选项卡
     */
    private RadioGroup radioGroup;
    /**
     * 当前表情包
     */
    private String emotion_title = "ac";
    /**
     * 表情页脚
     */
    private RadioGroup radioGroup1;
    /**
     * 表情页
     */
    private ViewPager viewPager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = View.inflate(getContext(), R.layout.fragment_emotion, null);
        radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(this);
        radioGroup1 = (RadioGroup) rootView.findViewById(R.id.radioGroup1);
        viewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
        radioGroup1.setOnCheckedChangeListener(this);
        viewPager.addOnPageChangeListener(this);

        try {
            init();
        } catch (IOException e) {
            e.printStackTrace();
            return rootView;
        }
        return rootView;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.radioGroup:
                switch (checkedId) {
                    case R.id.ac:
                        emotion_title = "ac";
                        break;
                    case R.id.dt:
                        emotion_title = "dt";
                        break;
                    case R.id.pst:
                        emotion_title = "pst";
                        break;
                }
                try {
                    init();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.radioGroup1:
                viewPager.setCurrentItem(checkedId);
                break;
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        radioGroup1.check(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    private void init() throws IOException {
        String[] fileNames;
        fileNames = getContext().getAssets().list("emotions/" + emotion_title);
        int count = fileNames.length;
        Log.i("emotion", "count::" + count);
        initRadioGroup(count);
        setEmotions(fileNames);
    }

    private void setEmotions(String[] fileNames) {
        List<Bitmap> bitmaps = new ArrayList<>();
        for (String string : fileNames) {
            InputStream is = null;
            try {
                is = getContext().getAssets().open("emotions/" + emotion_title + "/" + string);
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                bitmaps.add(bitmap);
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        viewPager.setAdapter(new EmotionGridView(bitmaps));
    }

    private void initRadioGroup(int count) {
        radioGroup1.removeAllViews();
        int pages = count / 13 + 1;
        for (int i = 0; i < pages; i++) {
            RadioButton button = new RadioButton(getContext());
            button.setButtonDrawable(R.drawable.emotion_radio_selector);
            button.setId(i);
            radioGroup1.addView(button, i, new RadioGroup.LayoutParams(0, RadioGroup.LayoutParams.MATCH_PARENT, 1));
        }
        radioGroup1.check(0);
    }


    private class EmotionGridView extends PagerAdapter {
        private List<Bitmap> bitmaps;

        public EmotionGridView(List<Bitmap> bitmaps) {
            this.bitmaps = bitmaps;
        }

        @Override
        public int getCount() {
            return bitmaps.size() / 13 + 1;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            GridView gridView = (GridView) View.inflate(getContext(), R.layout.fragment_emotion_gridview, null);
            int start = position * 12;
            int end = (position + 1) * 12;
            if (end > bitmaps.size()) {
                end = bitmaps.size();
            }
            gridView.setAdapter(new EmotionAdapter(bitmaps.subList(start, end)));
            container.addView(gridView);
            return gridView;
        }
    }

    private class EmotionAdapter extends BaseAdapter {
        private List<Bitmap> bitmaps;

        public EmotionAdapter(List<Bitmap> bitmaps) {
            this.bitmaps = bitmaps;
        }

        @Override
        public int getCount() {
            return bitmaps.size();
        }

        @Override
        public Object getItem(int position) {
            return bitmaps.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            ImageView imageView = new ImageView(getContext());
            imageView.setImageBitmap(bitmaps.get(position));
            imageView.setMinimumHeight(45);
            imageView.setMinimumWidth(45);
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            return imageView;
        }
    }

}
